<?php

class Soul_Template_Html_Form extends Soul_Template_Html_Container implements Soul_Template_Html_Data_IBinder{

	private $_title = '';
	private $_data = array();
	private $_hiddenSubmit = TRUE;
	private $_readonly = FALSE;
	private $_containerTag = 'table';
    private $_fields = array();
    
	public function __construct() {
		$this->setSetting('validate', TRUE)
		->setSetting('ajax', FALSE)
		->setMethod('post')
		->addClass('form');
	}
	
	public function getContainerTag(){
		return $this->_containerTag;
	}
	
	public function setContainerTag($value){
		$this->_containerTag = $value;
		return $this;
	}

	public function getData(){
		return $this->_data;
	}

	public function setData($value){
		$this->_data = $value;
		return $this;
	}

	public function getTitle(){
		return $this->_title;
	}

	public function setTitle($value){
		$this->_title = $value;
		return $this;
	}

	public function setEnctype($value) {
		return $this->setAttribute('enctype', $value);
	}

	public function getEnctype() {
		return $this->getAttribute('enctype');
	}

	public function getAction() {
		return $this->getAttribute('action');
	}

	public function setAction($value) {
		$this->setAttribute('action', $value);
		return $this;
	}

	public function getMethod() {
		return $this->getAttributes('method');
	}

	public function setMethod($value) {
		$this->setAttribute('method', $value);
		return $this;
	}
    
    public function getFields(){
        return $this->_fields;
    }
    
    public function getFieldById($id){
        foreach($this->_fields as $item){
            if($item->getId() == $id){
                return $item;
            }
        }
        return NULL;        
    }

	public function setReadonly($value){
		$this->_readonly = $value;
		return $this;
	}

	public function getReadonly(){
		return $this->_readonly;
	}

	public function getHiddenSubmit(){
		return $this->_hiddenSubmit;
	}

	public function setHiddenSubmit($value){
		$this->_hiddenSubmit = $value;
	}
    
    public function removeField($field){
        foreach($this->_fields as $index=>$item){
            if($item == $field){
                unset($this->_fields[$index]);
                return $field;
            }
        }
        return NULL;
    }
    
    public function removeFieldById($id){
        foreach($this->_fields as $index=>$item){
            if($item->getId() == $id){
                unset($this->_fields[$index]);
                return $item;
            }
        }
        return NULL;
    }    

	public function renderBegin(){
		return'<form ' . $this->getAttributeHtml() . '><'.$this->getContainerTag().' class="field-container">';
	}

	public function renderEnd(){
		$result = '</'.$this->getContainerTag().'>';
		if($this->getHiddenSubmit()){
			$result .= '<input type="submit" style="display:none"/>';
		}
		return $result . '</form>';
	}    
    
    public function addField($field){
        if(!$field->getId()){
            $field->setId('field_'.count($this->_fields));
        }
        $this->_fields[$field->getId()] = $field->setForm($this);
        return $this;
    }
	
	public function createField($label = '', $required = FALSE, $id = NULL){
		$result = new Soul_Template_Html_Form_Field();
        $result->setLabel($label)
               ->setRequired($required)
               ->setId($id)
               ->setForm($this);
        $this->addField($result);
        return $result;
	}    

	public function bind(Soul_Template_Html_Form_Input $input){
		if(count($this->getData()) > 0){
			$v = Soul_Util_Array::get($this->getData(), $input->getId(), NULL);
			$input->setValue($v);
		}
		return $input;
	}

	public function renderChildren(){
		$result = '';
        foreach($this->_fields as $field){
            $result .= $field->toHtml();
        }
        return $result;
	}
}